package com.example.ercmssystem2.controller;

import com.example.ercmssystem2.config.StatusConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 状态配置控制器
 * 提供状态映射的REST API接口
 */
@RestController
@RequestMapping("/api/status-config")
@CrossOrigin(origins = "*")
public class StatusConfigController {

    @Autowired
    private StatusConfig statusConfig;

    /**
     * 获取所有状态配置
     */
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAllStatusConfig() {
        Map<String, Object> config = new HashMap<>();
        config.put("eventStatuses", statusConfig.getAllEventStatuses());
        config.put("responderStatuses", statusConfig.getAllResponderStatuses());
        config.put("userStatuses", statusConfig.getAllUserStatuses());
        config.put("resourceStatuses", statusConfig.getAllResourceStatuses());
        config.put("safetyStatuses", statusConfig.getAllSafetyStatuses());
        config.put("priorities", statusConfig.getAllPriorities());
        config.put("severities", statusConfig.getAllSeverities());
        return ResponseEntity.ok(config);
    }

    /**
     * 获取事件状态映射
     */
    @GetMapping("/event-statuses")
    public ResponseEntity<Map<String, String>> getEventStatuses() {
        return ResponseEntity.ok(statusConfig.getAllEventStatuses());
    }

    /**
     * 获取响应者状态映射
     */
    @GetMapping("/responder-statuses")
    public ResponseEntity<Map<String, String>> getResponderStatuses() {
        return ResponseEntity.ok(statusConfig.getAllResponderStatuses());
    }

    /**
     * 获取用户状态映射
     */
    @GetMapping("/user-statuses")
    public ResponseEntity<Map<String, String>> getUserStatuses() {
        return ResponseEntity.ok(statusConfig.getAllUserStatuses());
    }

    /**
     * 获取资源状态映射
     */
    @GetMapping("/resource-statuses")
    public ResponseEntity<Map<String, String>> getResourceStatuses() {
        return ResponseEntity.ok(statusConfig.getAllResourceStatuses());
    }

    /**
     * 获取安全状态映射
     */
    @GetMapping("/safety-statuses")
    public ResponseEntity<Map<String, String>> getSafetyStatuses() {
        return ResponseEntity.ok(statusConfig.getAllSafetyStatuses());
    }

    /**
     * 获取优先级映射
     */
    @GetMapping("/priorities")
    public ResponseEntity<Map<String, String>> getPriorities() {
        return ResponseEntity.ok(statusConfig.getAllPriorities());
    }

    /**
     * 获取严重程度映射
     */
    @GetMapping("/severities")
    public ResponseEntity<Map<String, String>> getSeverities() {
        return ResponseEntity.ok(statusConfig.getAllSeverities());
    }

    /**
     * 获取事件状态中文名称
     */
    @GetMapping("/event-status/{status}")
    public ResponseEntity<Map<String, String>> getEventStatusText(@PathVariable String status) {
        Map<String, String> result = new HashMap<>();
        result.put("status", status);
        result.put("text", statusConfig.getEventStatusText(status));
        result.put("color", statusConfig.getStatusColor(status));
        result.put("cssClass", statusConfig.getStatusClass(status));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取响应者状态中文名称
     */
    @GetMapping("/responder-status/{status}")
    public ResponseEntity<Map<String, String>> getResponderStatusText(@PathVariable String status) {
        Map<String, String> result = new HashMap<>();
        result.put("status", status);
        result.put("text", statusConfig.getResponderStatusText(status));
        result.put("color", statusConfig.getStatusColor(status));
        result.put("cssClass", statusConfig.getStatusClass(status));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取用户状态中文名称
     */
    @GetMapping("/user-status/{status}")
    public ResponseEntity<Map<String, String>> getUserStatusText(@PathVariable String status) {
        Map<String, String> result = new HashMap<>();
        result.put("status", status);
        result.put("text", statusConfig.getUserStatusText(status));
        result.put("color", statusConfig.getStatusColor(status));
        result.put("cssClass", statusConfig.getStatusClass(status));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取资源状态中文名称
     */
    @GetMapping("/resource-status/{status}")
    public ResponseEntity<Map<String, String>> getResourceStatusText(@PathVariable String status) {
        Map<String, String> result = new HashMap<>();
        result.put("status", status);
        result.put("text", statusConfig.getResourceStatusText(status));
        result.put("color", statusConfig.getStatusColor(status));
        result.put("cssClass", statusConfig.getStatusClass(status));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取安全状态中文名称
     */
    @GetMapping("/safety-status/{status}")
    public ResponseEntity<Map<String, String>> getSafetyStatusText(@PathVariable String status) {
        Map<String, String> result = new HashMap<>();
        result.put("status", status);
        result.put("text", statusConfig.getSafetyStatusText(status));
        result.put("color", statusConfig.getStatusColor(status));
        result.put("cssClass", statusConfig.getStatusClass(status));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取优先级中文名称
     */
    @GetMapping("/priority/{priority}")
    public ResponseEntity<Map<String, String>> getPriorityText(@PathVariable String priority) {
        Map<String, String> result = new HashMap<>();
        result.put("priority", priority);
        result.put("text", statusConfig.getPriorityText(priority));
        result.put("color", statusConfig.getStatusColor(priority));
        result.put("cssClass", statusConfig.getStatusClass(priority));
        return ResponseEntity.ok(result);
    }

    /**
     * 获取严重程度中文名称
     */
    @GetMapping("/severity/{severity}")
    public ResponseEntity<Map<String, String>> getSeverityText(@PathVariable String severity) {
        Map<String, String> result = new HashMap<>();
        result.put("severity", severity);
        result.put("text", statusConfig.getSeverityText(severity));
        result.put("color", statusConfig.getStatusColor(severity));
        result.put("cssClass", statusConfig.getStatusClass(severity));
        return ResponseEntity.ok(result);
    }

    /**
     * 批量转换状态文本
     */
    @PostMapping("/batch-convert")
    public ResponseEntity<Map<String, Object>> batchConvertStatus(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();
        
        if (request.containsKey("eventStatus")) {
            String status = (String) request.get("eventStatus");
            result.put("eventStatus", statusConfig.getEventStatusText(status));
        }
        
        if (request.containsKey("responderStatus")) {
            String status = (String) request.get("responderStatus");
            result.put("responderStatus", statusConfig.getResponderStatusText(status));
        }
        
        if (request.containsKey("userStatus")) {
            String status = (String) request.get("userStatus");
            result.put("userStatus", statusConfig.getUserStatusText(status));
        }
        
        if (request.containsKey("resourceStatus")) {
            String status = (String) request.get("resourceStatus");
            result.put("resourceStatus", statusConfig.getResourceStatusText(status));
        }
        
        if (request.containsKey("safetyStatus")) {
            String status = (String) request.get("safetyStatus");
            result.put("safetyStatus", statusConfig.getSafetyStatusText(status));
        }
        
        if (request.containsKey("priority")) {
            String priority = (String) request.get("priority");
            result.put("priority", statusConfig.getPriorityText(priority));
        }
        
        if (request.containsKey("severity")) {
            String severity = (String) request.get("severity");
            result.put("severity", statusConfig.getSeverityText(severity));
        }
        
        return ResponseEntity.ok(result);
    }
} 